package com.ksr.logistics.mapper;

import com.ksr.logistics.pojo.Permission;
import com.ksr.logistics.pojo.PermissionExample;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PermissionMapper {
    int deleteByPrimaryKey(Long permissionId);

    int insert(Permission record);

    int insertSelective(Permission record);

    List<Permission> selectByExample(PermissionExample example);

    Permission selectByPrimaryKey(Long permissionId);

    int updateByPrimaryKeySelective(Permission record);

    int updateByPrimaryKey(Permission record);

    List<String> selByPermissionIds(@Param("ids") List<Long> ids);

    @Select("select t.*,#{permissionIds} ids from t_permission t where type='menu' and parent_id=#{parentId} and permission_id in(${permissionIds})")
    @Results({
        @Result(column = "{parentId=permission_id,permissionIds=ids}",property = "subMenus",many = @Many(select = "com.ksr.logistics.mapper.PermissionMapper.selMenus"))
    })
    List<Permission> selMenus(@Param("parentId") Long parentId,@Param("permissionIds") String permissionIds);

//    @Select("select * from t_permission where type='menu'")
//    List<Permission> selMenus();

    @Select("select p.*,#{roleId} roleId from t_permission p where type='menu' and parent_id=#{parentId} and permission_id in( " +
            "SELECT SUBSTRING_INDEX((SUBSTRING_INDEX((SELECT r.permission_ids from t_role r WHERE r.role_id=#{roleId} ),',',t.help_topic_id+1)),',',-1) ids " +
            "FROM mysql.help_topic t WHERE t.help_topic_id< " +
            "(LENGTH((SELECT r.permission_ids from t_role r WHERE r.role_id=#{roleId}))- " +
            "LENGTH(REPLACE((SELECT r.permission_ids from t_role r WHERE r.role_id=#{roleId}),',',''))+1)) order by p.sort")
    @Results({
            @Result(column = "{parentId=permission_id,roleId=roleId}",property = "subMenus",many = @Many(select = "com.ksr.logistics.mapper.PermissionMapper.getMenus"))
    })
    List<Permission> getMenus(@Param("parentId") Long parentId,@Param("roleId")Long roleId);
}